<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<%
   // 获取session创建时间
   Date createTime = new Date(session.getCreationTime());
   // 获取最后访问页面的时间
   Date lastAccessTime = new Date(session.getLastAccessedTime());

   String title = "session的例子";
   Integer visitCount = new Integer(0);
   String visitCountKey = new String("visitCount");
   String userIDKey = "userID";
   String userID = "ABCD";

   // 检测网页是否由新的访问用户
   if (session.isNew()){
      title = "设置 session";
     // map.put(key,value)
      session.setAttribute(userIDKey, userID);
      session.setAttribute(visitCountKey,  visitCount);
   } else {
	   title = "获取 session";
	   visitCount = (Integer)session.getAttribute(visitCountKey);
	   if(visitCount==null) {session.setAttribute(visitCountKey,0); visitCount=0;}
	   visitCount += 1;
	   userID = (String)session.getAttribute(userIDKey);
	   session.setAttribute(visitCountKey,  visitCount);
   }
%>
<html>
<head>
<title>Session 跟踪</title>
</head>
<body>

<h1>Session 跟踪</h1>

<table border="1" align="center"> 
<tr bgcolor="#949494">
   <th>Session 信息</th>
   <th>值</th>
</tr> 
<tr>
   <td>id</td>
   <td><% out.print( session.getId()); %></td>
</tr> 
<tr>
   <td>创建时间</td>
   <td><% out.print(createTime); %></td>
</tr> 
<tr>
   <td>最后访问时间</td>
   <td><% out.print(lastAccessTime); %></td>
</tr> 
<tr>
   <td>用户 ID</td>
   <td><% out.print(userID); %></td>
</tr> 
<tr>
   <td>访问次数</td>
   <td><% out.print(visitCount); %></td>
</tr> 

<%
 Enumeration headerNames =session.getAttributeNames() ;
 while(headerNames.hasMoreElements() ){
	
	 String paramName = (String)headerNames.nextElement();
     out.print("<tr><td> +" + paramName + "</td>\n");
     String paramValue = session.getAttribute(paramName).toString();
     out.println("<td> " + paramValue + "</td></tr>\n");
}
%>
</table> 
</body>
</html>